(function($, doc) {
	$.init();
	
	
	var lauchFlag = localStorage.getItem('lauchFlag');
	if(!lauchFlag) {
		$.openWindow({
			url: 'guide.html',
			id: 'guide',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}
	var settings = app.getSettings();
	var state = app.getState();
	var toMain = function() {
		location.href = 'main.html';
	};
	if(settings.gestures && settings.gestures != undefined) {
		location.href = 'pages/login/unlock.html';
	}
	var lastUser = app.getOneUser();
	if(!lastUser || !lastUser.user_id) {
		//切换做好后要跳到手机登录
		location.href = 'pages/login/loginbypwd.html';
	}
	if(state.user_id) {
		toMain();
	}
	$.plusReady(function() {
		console.log(plus.os.language);

		var langset = localStorage.getItem('defaultLan');
		if(!langset) {
			if(plus.os.language.indexOf('zh') === -1) {
				localStorage.setItem('defaultLan', 'en');
			} else {
				localStorage.setItem('defaultLan', 'zh');
			}

		}

		setTimeout(function() {
			//关闭 splash
			plus.navigator.closeSplashscreen();
			plus.nativeUI.closeWaiting();
		}, 100);
		plus.screen.lockOrientation("portrait-primary");

		//检查 "登录状态/锁屏状态" 结束
		var loginButton = doc.getElementById('login');
		var passwordBox = doc.getElementById('password');
		var footRow = doc.getElementById('footrow');
		var out_phone = lastUser.phone;
		var phones = out_phone.substr(0, 3) + ' ' + out_phone.substr(3, 4) + ' ' + out_phone.substr(-4);
		//doc.getElementById('phone').innerText = phones;
		//doc.getElementById('account').value = lastUser.phone;
		//doc.getElementById('avatar').src = app.avatar(lastUser.user_id, 'static/images/logo.png');
		//console.log(lastUser.avatar);
		/*
		passwordBox.addEventListener('input', function() {
			if(passwordBox.value != '') {
				loginButton.removeAttribute('disabled');
				//footRow.style.display = 'none';
			} else {
				loginButton.setAttribute('disabled', 'disabled');
				//footRow.style.display = '';
			}
		});*/
		mui('.mui-input-row').on('tap', '.mui-icon.mui-icon-clear', function() {
			document.getElementById('login').setAttribute('disabled', 'disabled');
		});

		function loginOp(event) {
			var loginInfo = {
				account: lastUser.phone,
				password: passwordBox.value,
				login_type: 1
			};
			if(loginInfo.password.length < 6) {
				plus.nativeUI.toast('密码最短为 6 个字符');
				return;
			}

			loginInfo.passwd = md5(loginInfo.password);
			ajaxlogin(loginInfo, 'main.html');
			/*
			if(loginInfo.passwd == lastUser.pwd) {
				app.createState(lastUser);
				toMain();
			} else {
				plus.nativeUI.toast('密码错误');
			}*/
			return;

		}
		//网页正文全文高
		var winheight = document.body.scrollHeight;

		function resizefooter(event) {
			var rheight = document.body.scrollHeight;
			if(winheight != rheight) {
				footRow.style.display = 'none';
			} else {
				footRow.style.display = '';
			}
		}
		loginButton.addEventListener('tap', loginOp, false);
		window.addEventListener('resize', resizefooter, false);
		doc.getElementById('sms_row').addEventListener('tap', function() {
			location.href = 'pages/login/loginbysms.html';
		}, false);
		doc.getElementById('forgetPassword').addEventListener('tap', function() {
			location.href = 'pages/login/find_pwd_list.html';
		}, false);
		doc.getElementById('moreto').addEventListener('tap', function() {
			var btns = [{
				title: "注册新帐号"
			}, {
				title: "切换账号"
			}];
			plus.nativeUI.actionSheet({
				title: "操作",
				cancel: "取消",
				buttons: btns
			}, function(e) {
				var id = e.index;
				if(id > 0) {
					//删除
					if(id == 1) {
						//location.href = "reg.html";
						mui.openWindow('reg.html', 'reg');
					} else if(id == 2) {
						location.href = "pages/login/loginbypwd.html";
					}
				}
				return;

			});
		}, false);
		var backButtonPress = 0;
		$.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
	});
}(mui, document));